import md5 from "md5";
import curd from "../../common/services/curd";
import {
  ADDCLASS,
  CREATECLASS,
  DELCLASS,
  EDITCLASS,
  EXITCLASS,
  GETCLASS,
  GETCLASSIDFROMCOMMANDWORD,
  GETCLASSINFO,
  GETONCLASS,
  GETPAPERTOTAL,
  GETSTUCLASS,
  GETSUBJECTSTOTAL,
  GETTOTALCLASS,
  ISCLASSEXIST,
  ISCLASSEXISTFROMCOMMANDWORD,
  ISCLASSEXISTFROMID,
  ISREPEATADDCLASS,
  ISUSERHASCLASS,
  REMOVECLASS,
} from "./sql";

class classService {
  /**
   * 根据班级口令获取对应的班级id
   */
  async getClassIdFromCommandWord(command_word: string) {
    return await curd.get(GETCLASSIDFROMCOMMANDWORD, command_word);
  }

  /**
   * 添加到班级
   * @param id 用户id
   * @param classId 班级ids
   */
  async addClass(id: string, classId: string) {
    return await curd.add(ADDCLASS, classId, id);
  }

  /**
   * 创建班级
   * @param name
   */
  async createClass(name: string, userId: string) {
    return await curd.add(CREATECLASS, name, md5(name), userId);
  }

  /**
   * 判断班级是否创建过了
   * @param name
   */
  async isClassExist(name: string) {
    return await curd.isExist(ISCLASSEXIST, name);
  }

  /**
   * 判断班级是否创建过了
   * @param command_word
   */
  async isClassExistByCommandWord(command_word: string) {
    return await curd.isExist(ISCLASSEXISTFROMCOMMANDWORD, command_word);
  }

  /**
   * 通过班级id判断班级是否创建过了
   * @param id
   */
  async isClassExistByID(id: string) {
    return await curd.isExist(ISCLASSEXISTFROMID, id);
  }

  /**
   * 将学生移除班级
   * @param id 学生id
   */
  async removeClass(classId: string, userId: string) {
    return await curd.delete(REMOVECLASS, classId, userId);
  }

  /**
   * 是否重复加入了班级
   * @param classId
   * @param userId
   */
  async isRepeatAddClass(classId: string, userId: string) {
    return await curd.isExist(ISREPEATADDCLASS, classId, userId);
  }

  /**
   * 获取所有的班级信息
   */
  async getClass(pageNum: number, pageSize: number, userId: string) {
    return await curd.get(GETCLASS, userId, pageNum, pageSize)
  }

  /**
   * 删除班级
   * @param id 
   */
  async delClass(classId: string, userId: string) {
    return await curd.delete(DELCLASS, classId, userId)
  }


  /**
   * 用户是否有该班级
   * @param classId 
   * @param userId 
   */
  async isUserHasClass(classId: string, userId: string) {
    return await curd.isExist(ISUSERHASCLASS, userId, classId)
  }

  /**
   * 获取一个班级的详情
   * @param id 
   */
  async getOne(id: string) {
    return await curd.get(GETONCLASS, id)
  }

  /**
   * 编辑班级信息
   * @param id 
   * @param name 
   */
  async editClass(id: string, name: string) {
    return await curd.update(EDITCLASS, name, id)
  }

  /**
   * 获取学生的班级
   * @param id 
   */
  async getStuClass(id: string) {
    return await curd.get(GETSTUCLASS, id)
  }

  /**
   * 退出班级
   * @param classId 
   * @param userId 
   */
  async exitStuClass(classId: string, userId: string) {
    return await curd.delete(EXITCLASS, classId, userId)
  }

  /**
   * 获取教师首页
   * @param userId 
   */
  async getTchInfo(userId: string) {
    let classInfo = await curd.get(GETCLASSINFO, userId)
    classInfo = classInfo.filter((info: any) => info.userId === userId)
    const paperInfo = await curd.get(GETPAPERTOTAL, userId)
    const subjects = await curd.get(GETSUBJECTSTOTAL)
    const totalClass = await curd.get(GETTOTALCLASS, userId)
    return {
      classInfo,
      paperInfo,
      subjects,
      totalClass
    }
  }

}
export default new classService();
